<!-- 配置项编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改课程/老师':'设置课程/老师'"
    :visible="visible"
    width="700px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px">

        <el-form-item
          label="科目课程:"
          prop="course_id">
          <el-select
            filterable
            clearable
            v-model="form.course_id"
            @change="changeTeacherList"
            size="small"
            placeholder="-请选择所属科目-"
            class="ele-block">
            <el-option v-for="item in subjectList" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
        </el-form-item>

        <el-form-item
          label="老师:"
          prop="teacher_id">
          <el-select
            filterable
            clearable
            v-model="form.teacher_id"
            @change="$forceUpdate()"
            size="small"
            placeholder="-请选择所属老师-"
            class="ele-block">
            <el-option v-for="item in teacherList" :key="item.id" :label="item.realname" :value="item.id"/>
          </el-select>
        </el-form-item>
            </el-form>
      <div slot="footer">
        <span v-if="data.id">
        <el-popconfirm
          class="ele-action"
          title="确定要删除此课程吗？"
          @onConfirm="remove(data)"
          @confirm="remove(data)">
          <el-button
            type="danger"
            slot="reference"
            :underline="false"
            icon="el-icon-delete">删除
          </el-button>
        </el-popconfirm>
        </span>
        <el-button @click="updateVisible(false)">取消</el-button>
        <el-button
          type="primary"
          @click="save"
          :loading="loading">确定
        </el-button>
      </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'ConfigDataEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 类型
    data: Object

  },
  data() {
    return {
      // 表单数据
      form: Object.assign({}, this.data),
      // 表单验证规则
      rules: {
        course_id: [
          {required: true, message: '请选择科目课程', trigger: 'blur'}
        ],
        // teacher_id: [
        //   {required: true, message: '请选择老师', trigger: 'blur'}
        // ]
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      subjectList:[],
      teacherList:[],
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
        // 获取科目列表
        this.getSubjectList();
      } else {
        this.form = {status: 1};
        this.isUpdate = false;
      }
    }
  },
  mounted() {
    // 获取科目列表
    this.getSubjectList();
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/curriculum/edit',
            Object.assign({}, this.form, {
              config_id: this.configId
            })
          ).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },

    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.post('/curriculum/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.updateVisible(false);
          this.$emit('done');
          // this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },

    /**
     * 获取科目列表
     */
    getSubjectList() {
      if(this.form.course_id){
        // this.form.grade = null;
        this.$forceUpdate();
        this.getTeacherList();
      }
      this.$http.post('/subject/getSubjectList',{cid:this.data.tid}).then(res => {
        if (res.data.code === 0) {
          this.subjectList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /**
     * 获取负责人列表
     */
    getTeacherList() {
      if(this.form.course_id){
        this.$forceUpdate();
      }
      this.$http.post('/user/getTeacherList',{cid:this.form.course_id,type:1,}).then(res => {
        if (res.data.code === 0) {
          this.teacherList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /**
     * 获取负责人列表
     */
    changeTeacherList() {
      if(this.form.teacher_id){
        this.form.teacher_id = null;
        this.$forceUpdate();
      }
      this.$http.post('/user/getTeacherList',{cid:this.form.course_id,type:1,}).then(res => {
        if (res.data.code === 0) {
          this.teacherList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
</style>
